package com.xbongbong.config;

import com.xbongbong.parent.interfaces.TeamStrategy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 团队改造策略的config
 * @author 魏荣杰
 *
 */
@Component
public class TeamStrategyConfig implements ApplicationContextAware {

    private Map<String, TeamStrategy> teamStrategyMap;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        teamStrategyMap = applicationContext.getBeansOfType(TeamStrategy.class);
    }

    @Bean("teamStrategyMap")
    public Map<Integer, TeamStrategy> initTeamStrategyMap() {
        Map<Integer, TeamStrategy> result = new HashMap<>();
        teamStrategyMap.forEach((key, item) -> item.type().forEach(v -> result.put(v, item)));
        return result;
    }
}
